<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/04/03
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\com;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use ImiApp\ApiServer\Api\Controller\Tpmecms;


use ImiApp\ApiServer\Api\Model\Goodssku;
use ImiApp\ApiServer\Api\Model\GoodsskuPrice;

/**
 * 公共店铺👔
 * @Auth(name="token",nologin={"get_store_data","get_store_data2"})
 * @Controller("/com.store/")
 */
class Store extends Tpmecms
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];





    /**
     * 获取店铺信息
     * @param int $id id
     * @return void
     * @Action
     */
    public function get_store_data($id = null)
    {
        // $store = Db::name('store')->where('id', '=', $id)->select()->get();

        $store = $this->getStoreData3($id);
        if (!$store) {
            return $this->error('没有该店铺');
        }
        if ($this->auth->isLogin()) //是否收藏店铺
            $store['islike'] = $this->IsShoucang(STORE, $id, $this->auth->user()->id);
        $data['store'] = $store;




        $where['status'] = '1'; // 0=关闭,1=正常
        $where['store_id'] = $id;
        $storebulletin = Db::name('storebulletin')->whereEx($where)->orderRaw('createtime desc')->page(1, 10)->select()->getArray();




        $data['storebulletin'] = $storebulletin;
        $this->AddViews($id);

        return $this->success('store', $data);
    }



    /**
     * 增加浏览量
     *
     * @param int $id
     * @return void
     */
    protected function AddViews($id)
    {
        Db::name('store')->where('id','=', $id)->setFieldInc('views')->update();
        if ($this->auth->isLogin())
            $this->AddViewlog(STORE, $id, $this->auth->user()->id);
    }

    /**
     * 获取店铺详细信息
     * @param int $id id
     * @param float $lat lat
     *  @param float $lng lng
     * @return void
     * @Action
     */
    public function get_store_data2($id = null, ?float $lat = null, ?float $lng = null)
    {
        $where['id'] = $id;

        $store = Db::name('store')
            ->whereEx($where)
            ->fieldRaw('close_status,qualification_images,declaration_id,name,logo_image,surety,credit,good,comments,ltdtype_status,ltd_status,city_switch,work_time,work_date,lat,lng,addr,content,banner_image,mobile,bbl_image,likes')
            ->select()
            ->get();
        $store['declaration'] = Db::name('self_declaration')->where('id','=',$store['declaration_id'])->value('name');
        $store['good_ratio'] = 100; //好评率
        if ($store['comments'] > 0) {
            $store['good_ratio'] = round($store['good'] / $store['comments'] * 100, 0);
        }
        $where2['store_id'] = $id;
        $where2['status'] = '2';
        $store['fgoodsbrand'] = Db::name('goodsbrand_apply')->whereEx($where2)->select()->getArray();
        // unset($store['bbl_image']);
        if ($this->auth->isLogin()) //是否收藏店铺
            $store['islike'] = $this->IsShoucang(STORE, $id, $this->auth->user()->id);
        if ($lat && $lng)
            $store['km'] = $this->get_coordinate_distance($lng, $lat, $store['lng'], $store['lat']);
            
        $this->AddViews($id);
        return $this->success('store', $store);
    }




    /**
     * 添加收藏店铺
     *
     * @param int $store_id 店铺id
     * @return void
     * @Action
     */
    public function add_likes($store_id)
    {
        $this->AddShoucang(STORE, $store_id, $this->auth->user()->id);
        $this->update_likes($store_id);
        return $this->success('收藏成功');
    }


    /**
     * 删除收藏店铺
     *
     * @param int $store_id 店铺id
     * @return void
     * @Action
     */
    public function del_likes($store_id)
    {
        $this->DelShoucang(STORE, $store_id, $this->auth->user()->id);
        $this->update_likes($store_id);
        return $this->success('取消成功');
    }

    /**
     * 更新收藏
     *
     * @param int $store_id 
     * @return void
     */
    protected function update_likes($store_id)
    {
        $data['typestatus_id'] = STORE;
        $data['public_id'] = $store_id;
        $count = Db::name('likes')->whereEx($data)->count();
        Db::name('store')->where('id', '=', $store_id)->update(['likes' => $count]);
    }

    /**
     * 获取收藏店铺列表
     *
     * @param float $lat lat
     * @param float $lng lng
     * @param integer $page 1
     * @param integer $limit 50
     * @return void
     * @Action
     */
    public function get_likes_store_list(?float $lat = null, ?float $lng = null, ?int $page = 1, ?int $limit = 50)
    {
        //查询店铺id
        $page = $page ?: 1;
        $limit = $limit ?: 50;

        $data['user_id'] = $this->auth->user()->id;
        $data['typestatus_id'] = STORE;

        $store_arr = Db::name('likes')->whereEx($data)->orderRaw('time desc')->column('public_id');
        $list = [];
        if ($store_arr)
            foreach ($store_arr as $key => $store_id) {

                $store = $this->getStoreData4($store_id);//店铺信息
                
                $whereGoods['apply_status'] = '2';//通过审核
                $whereGoods['store_id'] = $store_id;
                $whereGoods['status'] = 'up';
                $store['goods_list'] = Db::name('goods')
                // ->whereEx($this->whereCom)
                ->whereEx($whereGoods)
                ->orderRaw('createtime desc')
                ->whereIsNull('deletetime')
                ->page(1, 4)
                ->select()->getArray();

                if ($lat && $lng)
                    $store['km'] = $this->get_coordinate_distance($lng, $lat, $store['lng'], $store['lat']);
                $list[] = $store;
            }

        return $this->success('收藏店铺', $list);
    }




}
